package com.sugar.real.estate.module.business.service.designplan;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sugar.real.estate.framework.common.pojo.PageParam;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.module.business.controller.admin.designplan.vo.DesignPlanPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.designplan.vo.DesignPlanSaveReqVO;
import com.sugar.real.estate.module.business.controller.admin.designplan.vo.DesignPlanSimpleRespVO;
import com.sugar.real.estate.module.business.controller.app.designplan.vo.AppDesignPlanDetailRespVO;
import com.sugar.real.estate.module.business.controller.app.designplan.vo.AppDesignPlanPageRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.buildhouse.BuildHouseDO;
import com.sugar.real.estate.module.business.dal.dataobject.designplan.DesignPlanDO;
import com.sugar.real.estate.module.business.dal.mysql.buildhouse.BuildHouseMapper;
import com.sugar.real.estate.module.business.dal.mysql.designplan.DesignPlanMapper;
import com.sugar.real.estate.module.business.util.I18nUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.sugar.real.estate.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.real.estate.module.business.enums.ErrorCodeConstants.BUILD_HOUSE_NOT_EXISTS;
import static com.sugar.real.estate.module.business.enums.ErrorCodeConstants.DESIGN_PLAN_NOT_EXISTS;

/**
 * 设计方案 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class DesignPlanServiceImpl implements DesignPlanService {

    @Resource
    private DesignPlanMapper designPlanMapper;
    @Resource
    private BuildHouseMapper buildHouseMapper;

    @Override
    public Long createDesignPlan(DesignPlanSaveReqVO createReqVO) {
        // 插入
        DesignPlanDO designPlan = BeanUtils.toBean(createReqVO, DesignPlanDO.class);
        designPlanMapper.insert(designPlan);
        // 返回
        return designPlan.getId();
    }

    @Override
    public void updateDesignPlan(DesignPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateDesignPlanExists(updateReqVO.getId());
        // 更新
        DesignPlanDO updateObj = BeanUtils.toBean(updateReqVO, DesignPlanDO.class);
        designPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteDesignPlan(Long id) {
        // 校验存在
        validateDesignPlanExists(id);
        // 删除
        designPlanMapper.deleteById(id);
    }

    @Override
        public void deleteDesignPlanListByIds(List<Long> ids) {
        // 校验存在
        validateDesignPlanExists(ids);
        // 删除
        designPlanMapper.deleteByIds(ids);
        }

    private void validateDesignPlanExists(List<Long> ids) {
        List<DesignPlanDO> list = designPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DESIGN_PLAN_NOT_EXISTS);
        }
    }

    private void validateDesignPlanExists(Long id) {
        if (designPlanMapper.selectById(id) == null) {
            throw exception(DESIGN_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public DesignPlanDO getDesignPlan(Long id) {
        return designPlanMapper.selectById(id);
    }

    @Override
    public PageResult<DesignPlanDO> getDesignPlanPage(DesignPlanPageReqVO pageReqVO) {
        return designPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DesignPlanSimpleRespVO> getDesignPlanAll() {
        List<DesignPlanDO> plans = designPlanMapper.selectList();
        return BeanUtils.toBean(plans, DesignPlanSimpleRespVO.class, o -> o.setTitle(o.getTitleZhCn() + "(" + o.getTitleEnUs() + ")"));
    }

    @Override
    public PageResult<AppDesignPlanPageRespVO> appGetDesignPlanPage(PageParam reqVO) {
        return BeanUtils.toBean(designPlanMapper.appSelectPage(reqVO), AppDesignPlanPageRespVO.class,
                o -> o.setTitle(I18nUtil.getMessage(o, "getTitle"))
                        .setShowImage(o.getShowImages().get(0)));
    }

    @Override
    public List<AppDesignPlanPageRespVO> getDesignPlanListByBuildHouseId(Long buildHouseId) {
        BuildHouseDO buildHouseDO = buildHouseMapper.selectById(buildHouseId);
        if (Objects.isNull(buildHouseDO))
            throw exception(BUILD_HOUSE_NOT_EXISTS, I18nUtil.getMessage("BUILD_HOUSE_NOT_EXISTS"));
        List<Long> planIds = buildHouseDO.getPlanIds();
        if (CollUtil.isNotEmpty(planIds)){
            List<DesignPlanDO> plans = designPlanMapper.selectList(Wrappers.<DesignPlanDO>lambdaQuery().in(DesignPlanDO::getId, planIds));
            return BeanUtils.toBean(plans, AppDesignPlanPageRespVO.class, o -> o
                    .setTitle(I18nUtil.getMessage(o, "getTitle"))
                    .setShowImage(o.getShowImages().get(0)));
        }
        return Collections.emptyList();
    }

    @Override
    public AppDesignPlanDetailRespVO appGetDesignPlan(Long id) {
        DesignPlanDO designPlanDO = designPlanMapper.selectById(id);
        AppDesignPlanDetailRespVO respVO = BeanUtils.toBean(designPlanDO, AppDesignPlanDetailRespVO.class);

        respVO.setTitle(I18nUtil.getMessage(respVO, "getTitle"))
                .setContent(I18nUtil.getMessage(respVO, "getContent"));

        return respVO;
    }
}